//Page Object
import {yanZ,login} from '../../api/user'

Page({
  data: {
    loginForm:{
      mobile:'',
      code:''
    },
    num:60,
    flag:true,
    error:false,
    errText:'手机号格式错误',
    errorCode:false,
    errCode:'验证码格式错误'
  },

  addMobile(e) {
    // console.log(e);
    this.setData({
      'loginForm.mobile':e.detail.value
    })
  },

  addcode(e){
    this.setData({
      'loginForm.code':e.detail.value
    })
  },

  async sendYz() {
    if(this.bindblur()===true){ 
      let sum=59
    const timer= setInterval(() => {
       this.setData({
         num:sum--
       })
       if(sum===-1) {
         clearTimeout(timer)
         sum=3
         this.setData({
           flag:true,
           num:60,
         })
        }
     }, 1000);  
     try {
      const res= await yanZ(+this.data.loginForm.mobile)
      this.setData({
        flag:false,
      })
      this.setData({
        'loginForm.code':'246810'
      })
      wx.showToast({
        title:'验证码获取成功',
        icon:'success'
      })
  //  console.log(res);
    } catch (error) {
   wx.showToast({
    title:error.response.data.message,
    icon:'none'
  })
  clearTimeout(timer)
  this.setData({
    flag:true,
    num:60,
   
  }) 
    }
    }
  },
 async login () {
  if( this.bindblurCode()===true , this.bindblur()===true) {
    try {
     const {data:{token}} = await login(this.data.loginForm)
    //  console.log(token)
     wx.setStorage({
      key:"token",
      data:token
    })
    wx.setStorage({
      key:"ID",
      data:+this.data.loginForm.mobile
    })
      wx.showToast({
        title:'登录成功',
        icon:'success'
      })
      wx.switchTab({
        url:'/pages/main/main'
      })
    } catch (error) {
      wx.showToast({
        title:error.response.data.message,
        icon:'none'
      })
    }
  }
     
   
  
  
  
  },
  //手机号表单校验
  bindblur() {
    const reg =/^(?:(?:\+|00)86)?1\d{10}$/
    if(this.data.loginForm.mobile.trim()===''){
      this.setData({
        error: true,
        errText:'请输入手机号'
      })
      return new Error('错误')
    }
    else if (!reg.test(this.data.loginForm.mobile.trim())) {
    // console.log(1);
      this.setData({
        error: true,
        errText:'手机号格式错误'
      })
      return new Error('错误') 
    } else {
      this.setData({
        error: false,
      })
      return true
    }
  },

  //验证码表单校验
  bindblurCode () {
    const reg = /^\d{6}$/
    if(this.data.loginForm.code.trim()===''){
      this.setData({
        errorCode: true,
        errCode:'请输入验证码'
      })
      return new Error('错误')
    }
    else if (!reg.test(this.data.loginForm.code.trim())) {
      this.setData({
        errorCode: true,
        errCode:'验证码格式错误'
      })
      return new Error('错误')
    } else {
      this.setData({
        errorCode: false,
      })
      return true
    }
  },

















  onLoad: function(options){
    
  },
  onReady: function(){
    
  },
  onShow: function(){
    
  },
  onHide: function(){

  },
  onUnload: function(){

  },
  onPullDownRefresh: function(){

  },
  onReachBottom: function(){

  },
  onShareAppMessage: function(){

  },
  onPageScroll: function(){

  },
  //item(index,pagePath,text)
  onTabItemTap:function(item){

  }
});